package items
{
	import org.flixel.*;
	
	import enemies.*;
	
	/**
	 * ...
	 * @author JAC
	 */
	public class Slow extends Item
	{
		private static const SLOW_AMOUNT:Number = 200;
		
		private var waveUsedOn:int;
		
		public function Slow(i:uint = 0, j:uint = 0)
		{
			super(i, j);
			
			_duration = 3;
			
			loadGraphic(Assets.ImgItemClock);
			
			toggleLight();
		}
		
		override public function onUse(p:Player):Boolean
		{
			super.onUse(p);
			
			waveUsedOn = Tracker.wave;
			
			for (var i:uint = 0; i < Registry.enemies.length; i++)
			{
				var e:Enemy = Registry.enemies.members[i];
				if (e.alive)
				{
					e.offsetSpeed(-SLOW_AMOUNT, true);
				}
			}
			
			FlxG.play(Assets.SndSlowDown, Registry.soundVolume);
			FlxG.flash(0xFFFFFFFF, 0.1);
			_timer.reset(_duration * 1000);
			
			return true;
		}
		
		override public function update():void
		{
			super.update();
			
			if (_used)
			{
				if (_timer.hasExpired || !Player.isAlive)
				{
					FlxG.play(Assets.SndSpeedUp, Registry.soundVolume);
					if (waveUsedOn == Tracker.wave)
					{
						for (var i:uint = 0; i < Registry.enemies.length; i++)
						{
							var e:Enemy = Registry.enemies.members[i];
							if (e.alive)
							{
								e.offsetSpeed(SLOW_AMOUNT, true);
							}
						}
					}
					destroy();
					return;
				}
			}
		}
	}
}